{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'refresh_token': '25.b74682e9075927922b5a5fdaf3b362d2.315360000.1918910679.282335-22866183',\n",
       " 'expires_in': 2592000,\n",
       " 'session_key': '9mzdCP0Ce6QtNzPxb+RlNBhSgG7US16ICOsFogB+jsSE2DORyziVyt2x1WY9CifBW5gtPJwSdJpmuWWRa7KO65QHbbCydw==',\n",
       " 'access_token': '24.397e44d22ec40d57bbbb842b8f55d9ed.2592000.1606142679.282335-22866183',\n",
       " 'scope': 'public brain_all_scope vis-faceverify_faceverify_h5-face-liveness vis-faceverify_FACE_V3 vis-faceverify_idl_face_merge vis-faceverify_FACE_EFFECT vis-faceverify_face_feature_sdk wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component',\n",
       " 'session_secret': '544a9d4e9e2873f16faea00d01fd9849'}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests \n",
    "\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'\n",
    "client_id = \"7B94tQKN9ngRqlDKz6UVeYpW\"\n",
    "client_secret = \"f6VgAsr9QQ8ZRbZ09FHTarX4ZFHwcyFv\"\n",
    "response = requests.get(host.format(client_id, client_secret))\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 3510155001996,\n",
       " 'timestamp': 1603550713,\n",
       " 'cached': 0,\n",
       " 'result': {'face_num': 1,\n",
       "  'face_list': [{'face_token': '32916371ced0b40df6376c86daa6b6ce',\n",
       "    'location': {'left': 218.77,\n",
       "     'top': 136.25,\n",
       "     'width': 265,\n",
       "     'height': 260,\n",
       "     'rotation': -13},\n",
       "    'face_probability': 1,\n",
       "    'angle': {'yaw': 11.91, 'pitch': 5.18, 'roll': -18.2},\n",
       "    'face_shape': {'type': 'oval', 'probability': 0.5},\n",
       "    'face_type': {'type': 'human', 'probability': 0.96}}]}}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "\n",
    "params = \"{\\\"image\\\":\\\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603560744147&di=e7c2de24f4e234bb1b78856aa9940336&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Ftranslate%2F325%2Fw720h405%2F20181021%2FXECE-hmuuiyv4775222.jpg\\\",\\\"image_type\\\":\\\"URL\\\",\\\"face_field\\\":\\\"faceshape,facetype\\\"}\"\n",
    "\n",
    "access_token = '24.397e44d22ec40d57bbbb842b8f55d9ed.2592000.1606142679.282335-22866183' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603560744147&di=e7c2de24f4e234bb1b78856aa9940336&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Ftranslate%2F325%2Fw720h405%2F20181021%2FXECE-hmuuiyv4775222.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 1599848465353,\n",
       " 'timestamp': 1603550859,\n",
       " 'cached': 0,\n",
       " 'result': {'score': 68.47779846,\n",
       "  'face_list': [{'face_token': '32916371ced0b40df6376c86daa6b6ce'},\n",
       "   {'face_token': '95bfe3a2d4f8652893871b3b62120831'}]}}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/match\"\n",
    "\n",
    "params = \"[{\\\"image\\\": \\\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603560744147&di=e7c2de24f4e234bb1b78856aa9940336&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Ftranslate%2F325%2Fw720h405%2F20181021%2FXECE-hmuuiyv4775222.jpg\\\", \\\"image_type\\\": \\\"URL\\\", \\\"face_type\\\": \\\"CERT\\\", \\\"quality_control\\\": \\\"LOW\\\"}, {\\\"image\\\": \\\"https://gitee.com/weihongfen_newmedia/api/raw/master/images/%E6%A3%9A%E6%8B%8D%E7%9A%84%E7%85%A7%E7%89%87.jpg\\\", \\\"image_type\\\": \\\"URL\\\", \\\"face_type\\\": \\\"LIVE\\\", \\\"quality_control\\\": \\\"LOW\\\"}]\"\n",
    "\n",
    "access_token = '24.0377f8b94ec266279bd33bddf7b8cd40.2592000.1606034086.282335-22866183' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://gitee.com/weihongfen_newmedia/api/raw/master/images/%E6%A3%9A%E6%8B%8D%E7%9A%84%E7%85%A7%E7%89%87.jpg)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
